package com.cloudsigns.factorymanager2.project.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;

import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IUploadPhotoView;

import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;

import com.cloudsigns.factorymanager2.engineering.bean.DialogItemBean;

import com.cloudsigns.factorymanager2.engineering.customview.BottomListDialog;

import com.cloudsigns.factorymanager2.engineering.presenter.UploadPhotoPresenter;
import com.cloudsigns.factorymanager2.engineering.utils.PhotoUtil;

import com.cloudsigns.factorymanager2.project.bean.SurVeyOrderBean;
import com.cloudsigns.factorymanager2.project.bean.SurveyFeedBackForSizeBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by tony on 2018/8/11.
 */

public class SurveyAddSizeFeedBackActivity extends Activity implements IUploadPhotoView, View.OnClickListener, AdapterView.OnItemClickListener, BottomListDialog.OnItemDialogClick {
    private final String TAG = SurveyAddSizeFeedBackActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private RadioGroup rg_feed_back_type;
    private RadioButton rb_normal_feedback, rb_unusual_feedback;
    private TextView btn_upload;
    private EditText edt_survey_content;
    private View ll_normal_feedback, ll_special_feedback;
    private EditText edt_width, edt_height, edt_corner, edt_hightDif;
    private EditText edt_specialWidth, edt_specialHight, edt_arcLength, edt_arcHight;
    private GridView gridView_size, gridView_special;
    private GridAdapter gridViewNormalAdapter, gridViewSpecialAdapter;
    private List<String> normalPathList = new ArrayList<>();
    private List<String> specialPathList = new ArrayList<>();
    private List<String> serverNormalUrlList = new ArrayList<>();
    private List<String> serverSpecialUrlList = new ArrayList<>();
    private String filePath;
    private final int NORMAL_FEEDBACK = 1;
    private final int UNSUAL_FEEDBACK = 2;
    private int feedBackType = NORMAL_FEEDBACK;
    private SurVeyOrderBean surVeyOrderBean;
    private UploadPhotoPresenter uploadPhotoPresenter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_survey_add_size_feedback);

        uploadPhotoPresenter = new UploadPhotoPresenter(this);
        init();
        tv_title.setText("尺寸记录");
        updateUI();


        if (null != getIntent()) {
            surVeyOrderBean = (SurVeyOrderBean) getIntent().getSerializableExtra(ConstantUtil.KEY_1);
        }

    }

    private void init() {
        rl_back = (RelativeLayout) findViewById(R.id.rl_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
        rg_feed_back_type = (RadioGroup) findViewById(R.id.rg_feed_back_type);
        rb_normal_feedback = (RadioButton) findViewById(R.id.rb_normal_feedback);
        rb_unusual_feedback = (RadioButton) findViewById(R.id.rb_unusual_feedback);
        btn_upload = (TextView) findViewById(R.id.btn_upload);
        ll_normal_feedback = findViewById(R.id.ll_normal_feedback);
        ll_special_feedback = findViewById(R.id.ll_special_feedback);
        edt_width = findViewById(R.id.edt_width);
        edt_height = findViewById(R.id.edt_height);
        edt_corner = findViewById(R.id.edt_corner);
        edt_hightDif = findViewById(R.id.edt_hightDif);
        edt_specialWidth = findViewById(R.id.edt_specialWidth);
        edt_specialHight = findViewById(R.id.edt_specialHight);
        edt_arcLength = findViewById(R.id.edt_arcLength);
        edt_arcHight = findViewById(R.id.edt_arcHight);
        edt_survey_content = findViewById(R.id.edt_survey_content);

        gridView_size = findViewById(R.id.gridView_size);
        gridView_special = findViewById(R.id.gridView_special);

        normalPathList.add("paizhao");
        specialPathList.add("paizhao");

        gridView_size.setNumColumns(3);
        gridView_special.setNumColumns(3);

        gridViewNormalAdapter = new GridAdapter(this);
        gridViewNormalAdapter.setData(normalPathList);

        gridViewSpecialAdapter = new GridAdapter(this);
        gridViewSpecialAdapter.setData(specialPathList);

        gridView_size.setAdapter(gridViewNormalAdapter);
        gridView_special.setAdapter(gridViewSpecialAdapter);

        gridView_size.setOnItemClickListener(this);
        gridView_special.setOnItemClickListener(this);

        rl_back.setOnClickListener(this);
        btn_upload.setOnClickListener(this);


        rb_normal_feedback.setChecked(true);
        rg_feed_back_type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int id) {
                if (id == R.id.rb_normal_feedback) {
                    feedBackType = NORMAL_FEEDBACK;
                    updateUI();

                } else if (id == R.id.rb_unusual_feedback) {
                    feedBackType = UNSUAL_FEEDBACK;
                    updateUI();
                }
            }
        });


    }

    private void updateUI() {
        if (feedBackType == NORMAL_FEEDBACK) {
            ll_normal_feedback.setVisibility(View.VISIBLE);
            ll_special_feedback.setVisibility(View.GONE);
        } else if (feedBackType == UNSUAL_FEEDBACK) {
            ll_normal_feedback.setVisibility(View.GONE);
            ll_special_feedback.setVisibility(View.VISIBLE);
        }
    }

    private void showListDialog() {
        DialogItemBean[] items = new DialogItemBean[]{
                new DialogItemBean(R.string.camera,
                        R.layout.dialog_list_item_normal),
                new DialogItemBean(R.string.gallary,
                        R.layout.dialog_list_item_normal),
                new DialogItemBean(R.string.cancel,
                        R.layout.dialog_list_item_normal)};
        BottomListDialog dialog = new BottomListDialog(this, Arrays.asList(items));
        dialog.setItemClick(this);
        dialog.show();
    }


    private void submitFeedBack() {
        if (feedBackType == NORMAL_FEEDBACK) {
            SurveyFeedBackForSizeBean surveyFeedBackForSizeBean = new SurveyFeedBackForSizeBean();
            surveyFeedBackForSizeBean.setContent(edt_survey_content.getText().toString().trim());

            String widthStr = edt_width.getText().toString().trim();
            if (!TextUtils.isEmpty(widthStr))
                surveyFeedBackForSizeBean.setWidth(Integer.parseInt(widthStr));

            String heightStr = edt_height.getText().toString().trim();
            if (!TextUtils.isEmpty(heightStr))
                surveyFeedBackForSizeBean.setHight(Integer.parseInt(heightStr));

            String cornertr = edt_corner.getText().toString().trim();
            if (!TextUtils.isEmpty(cornertr))
                surveyFeedBackForSizeBean.setCorner(Integer.parseInt(cornertr));

            String hightDifStr = edt_hightDif.getText().toString().trim();
            if (!TextUtils.isEmpty(hightDifStr))
                surveyFeedBackForSizeBean.setHightDif(Integer.parseInt(hightDifStr));

            surveyFeedBackForSizeBean.setImgUrlList(serverNormalUrlList);
            surveyFeedBackForSizeBean.setSurveySizeType(CommonEnum.SurveySizeType.normal);

            Toast.makeText(this, "上传成功！", Toast.LENGTH_SHORT).show();
            Intent mIntent = new Intent();
            mIntent.putExtra(ConstantUtil.KEY_1, surveyFeedBackForSizeBean);
            setResult(RESULT_OK, mIntent);
            finish();
        } else if (feedBackType == UNSUAL_FEEDBACK) {

            SurveyFeedBackForSizeBean surveyFeedBackForSizeBean = new SurveyFeedBackForSizeBean();
            surveyFeedBackForSizeBean.setContent(edt_survey_content.getText().toString().trim());

            String specialWidthStr = edt_specialWidth.getText().toString().trim();
            if (!TextUtils.isEmpty(specialWidthStr))
                surveyFeedBackForSizeBean.setSpecialWidth(Integer.parseInt(specialWidthStr));

            String specialHightStr = edt_specialHight.getText().toString().trim();
            if (!TextUtils.isEmpty(specialHightStr))
                surveyFeedBackForSizeBean.setSpecialHight(Integer.parseInt(specialHightStr));

            String arcLengthStr = edt_arcLength.getText().toString().trim();
            if (!TextUtils.isEmpty(arcLengthStr))
                surveyFeedBackForSizeBean.setArcLength(Integer.parseInt(arcLengthStr));

            String arcHightStr = edt_arcHight.getText().toString().trim();
            if (!TextUtils.isEmpty(arcHightStr))
                surveyFeedBackForSizeBean.setArcHight(Integer.parseInt(arcHightStr));

            surveyFeedBackForSizeBean.setImgUrlList(serverSpecialUrlList);
            surveyFeedBackForSizeBean.setSurveySizeType(CommonEnum.SurveySizeType.special);

            Toast.makeText(this, "上传成功！", Toast.LENGTH_SHORT).show();
            Intent mIntent = new Intent();
            mIntent.putExtra(ConstantUtil.KEY_1, surveyFeedBackForSizeBean);
            setResult(RESULT_OK, mIntent);
            finish();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        String imgs = (String) adapterView.getItemAtPosition(i);
        if ("paizhao".equals(imgs)) {
            showListDialog();
        }
//        if(adapterView == gridView_size){
//
//        }else if(adapterView == gridView_special){
//
//        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_upload:
                submitFeedBack();
                break;
            case R.id.rl_back:
                finish();
                break;

        }
    }

    @Override
    public void onItemDialogClick(DialogItemBean item) {

        switch (item.getmTextId()) {
            case R.string.camera: {

                PhotoUtil.startCamear(SurveyAddSizeFeedBackActivity.this, null);
                break;
            }
            case R.string.gallary: {
                PhotoUtil.startGallery(SurveyAddSizeFeedBackActivity.this, null);
                break;
            }
            default:
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {

            case PhotoUtil.CAMERA:
                if (resultCode == RESULT_OK) {
                    File picture = new File(PhotoUtil.filePath);
                    if (!picture.exists()) {
                        picture = new File(PhotoUtil.filePath);
                    }

                    filePath = PhotoUtil.CAMERA_SEVER + picture.getName();
                    filePath = PhotoUtil.imageCompressionAndSave(picture.getAbsolutePath(), filePath);
                    //override(100, 100)
                    if (feedBackType == NORMAL_FEEDBACK) {
                        if (normalPathList.contains("paizhao")) {
                            normalPathList.remove("paizhao");
                        }
                        if (normalPathList.size() < 3) {
                            normalPathList.add(filePath);
                            if (normalPathList.size() < 3)
                                normalPathList.add("paizhao");

                            gridViewNormalAdapter.setData(normalPathList);
                            gridViewNormalAdapter.notifyDataSetChanged();
                        }
                    } else if (feedBackType == UNSUAL_FEEDBACK) {
                        if (specialPathList.contains("paizhao")) {
                            specialPathList.remove("paizhao");
                        }
                        if (specialPathList.size() < 3) {
                            specialPathList.add(filePath);
                            if (specialPathList.size() < 3)
                                specialPathList.add("paizhao");

                            gridViewSpecialAdapter.setData(specialPathList);
                            gridViewSpecialAdapter.notifyDataSetChanged();
                        }

                    }


                    uploadPhotoPresenter.uploadPhtoto();

                }

                break;
            case PhotoUtil.GALLERY:
                if (resultCode == RESULT_OK) {

                    String path = FileUtil.getRealPathFromURI(this, Uri.parse(data.getData() + ""));

                    if (feedBackType == NORMAL_FEEDBACK) {
                        if (normalPathList.contains("paizhao")) {
                            normalPathList.remove("paizhao");
                        }
                        if (normalPathList.size() < 3) {
                            normalPathList.add(path);
                            if (normalPathList.size() < 3)
                                normalPathList.add("paizhao");

                            gridViewNormalAdapter.setData(normalPathList);
                            gridViewNormalAdapter.notifyDataSetChanged();
                        }
                    } else if (feedBackType == UNSUAL_FEEDBACK) {
                        if (specialPathList.contains("paizhao")) {
                            specialPathList.remove("paizhao");
                        }
                        if (specialPathList.size() < 3) {
                            specialPathList.add(path);
                            if (specialPathList.size() < 3)
                                specialPathList.add("paizhao");

                            gridViewSpecialAdapter.setData(specialPathList);
                            gridViewSpecialAdapter.notifyDataSetChanged();
                        }

                    }


                    File f = new File(path);
                    filePath = PhotoUtil.CAMERA_SEVER + f.getName();
                    filePath = PhotoUtil.imageCompressionAndSave(path, filePath);
                    uploadPhotoPresenter.uploadPhtoto();

                }
                break;
            case PhotoUtil.PHOTORESOULT:
                break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public String getFilePath() {
        return filePath;
    }


    @Override
    public void upLoadPhotoSuccessed(String uri) {
        if (feedBackType == NORMAL_FEEDBACK) {
            if (serverNormalUrlList.size() < 3)
                serverNormalUrlList.add(uri);
        } else if (feedBackType == UNSUAL_FEEDBACK) {
            if (serverSpecialUrlList.size() < 3)
                serverSpecialUrlList.add(uri);

        }

    }

    @Override
    public void upLoadPhotoFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
